/******************************************************************************/
package jamie.org.sil.www.usxUtils;

import java.util.TreeMap;



/******************************************************************************/
/**
   Numerical processor to handle number systems which are basically like the
   standard European system, but which use different glyphs (and possibly
   a different number base), and where the most significant digit is at
   the <i>right</i>.
*/

public class NumeralProcessor_Positional_Rtl extends NumeralProcessor
{
  /****************************************************************************/
  /****************************************************************************/
  /**                                                                        **/
  /**                               Public                                   **/
  /**                                                                        **/
  /****************************************************************************/
  /****************************************************************************/

  /****************************************************************************/
  /**
     Initialise the new instance.

     @param integerToGlyph Decimal number to vernacular character mapping.

     @param textDir LTR / RTL etc.

     @param base Number base.
  */

  @Override public void initialise (TreeMap<Integer, String> integerToGlyph,
                                    String textDir,
                                    int base)
  {
    super.initialise(integerToGlyph, textDir, base);
    m_Base = integerToGlyph.size();
  }

  
  /****************************************************************************/
  /**
     @{inheritDoc}
  */

  @Override public int fromVernacular (String n)
  {
    int res = 0;
    for (int i = n.length() - 1; i >= 0; --i)
    {
      res *= m_Base;
      //res += m_GlyphToInteger.get(n.substring(i, i+1));
      res += Character.digit(n.charAt(i), 10);
    }

    return res;
  }


  /****************************************************************************/
  /**
     @{inheritDoc}
  */

  @Override public void setInstance ()
  {
    m_Instance = this;
  }


  /****************************************************************************/
  /**
     @{inheritDoc}
  */

  @Override public String toVernacular (int n)
  {
    if (0 == n) return m_IntegerToGlyph.get(0);

    String res = "";
    while (n > 0)
    {
      res = res + m_IntegerToGlyph.get(n % m_Base);
      n /= m_Base;
    }

    return res;
  }
}
